﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using AmiCheckin.Code.Util;
using AmiCheckin.Lib.Model;
using PropertyChanged;

namespace AmiCheckin.UI.ViewModels
{
    [AddINotifyPropertyChangedInterface]
    public class RewardLocationViewModel
    {
        public ObservableCollection<RewardLocationObject> LocationObjects { get; set; }

        public void Load()
        {
            var config = ConfigUtil.GetConfig();

            LocationObjects = new(config.RewardTypes.Select(x =>
            {
                return new RewardLocationObject()
                {
                    Name = x.Key,
                    Path = x.Value.Path,
                    Args = new(x.Value.Args),
                    LocationType = x.Value.LocationType.ToString()
                };
            }));
        }

        public void Save()
        {
            var config = ConfigUtil.GetConfig();

            var dic = config.RewardTypes;
            dic.Clear();
            foreach (var item in LocationObjects)
            {
                dic.Add(item.Name, new RewardLocation()
                {
                    Args = item.Args.ToList(),
                    LocationType = (LocationType)Enum.Parse(typeof(LocationType), item.LocationType, true),
                    Path = item.Path
                }); ;
            }
            config.RewardTypes = dic;
            ConfigUtil.SetConfig(config);
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class RewardLocationObject
    {
        public string Name { get; set; }
        public string LocationType { get; set; }
        public string Path { get; set; }
        public ObservableCollection<string> Args { get; set; } = new();
    }
}